﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace jQueryDotNetLib.Extenders
{
    public enum AxisOptions{x, y, none};
    public enum RevertOptions { yes, no, invalid, valid };
    public enum SnapModes { inner, outer, both }

    [ToolboxItem(true)]
    [ToolboxData("<{0}:Draggable Selector=\"\" runat=server />")]
    public class Draggable : BaseInteractionExtender
    {
        public Draggable()
        {
            Interaction = "draggable";
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDragStart
        {
            get
            {
                if (ViewState["OnClientDragStart"] == null) return "";
                return (string)ViewState["OnClientDragStart"];
            }
            set
            {
                ViewState["OnClientDragStart"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDrag
        {
            get
            {
                if (ViewState["OnClientDrag"] == null) return "";
                return (string)ViewState["OnClientDrag"];
            }
            set
            {
                ViewState["OnClientDrag"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDragStop
        {
            get
            {
                if (ViewState["OnClientDragStop"] == null) return "";
                return (string)ViewState["OnClientDragStop"];
            }
            set
            {
                ViewState["OnClientDragStop"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AddClasses
        {
            get
            {
                if (ViewState["AddClasses"] == null) return true;
                return (bool)ViewState["AddClasses"];
            }

            set
            {
                ViewState["AddClasses"] = value;
            }
        }
        
        
        
        
        
        
        
        
        
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AppendTo
        {
            get
            {
                if (ViewState["AppendTo"] == null) return "";
                return (string)ViewState["AppendTo"];
            }

            set
            {
                ViewState["AppendTo"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public AxisOptions Axis
        {
            get
            {
                if (ViewState["Axis"] == null) return AxisOptions.none;
                return (AxisOptions)ViewState["Axis"];
            }

            set
            {
                ViewState["Axis"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cancel
        {
            get
            {
                if (ViewState["Cancel"] == null) return "";
                return (string)ViewState["Cancel"];
            }

            set
            {
                ViewState["Cancel"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ConnectToSortable
        {
            get
            {
                if (ViewState["ConnectToSortable"] == null) return "";
                return (string)ViewState["ConnectToSortable"];
            }

            set
            {
                ViewState["ConnectToSortable"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Containment
        {
            get
            {
                if (ViewState["Containment"] == null) return "";
                return (string)ViewState["Containment"];
            }

            set
            {
                ViewState["Containment"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cursor
        {
            get
            {
                if (ViewState["Cursor"] == null) return "";
                return (string)ViewState["Cursor"];
            }

            set
            {
                ViewState["Cursor"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CursorAt
        {
            get
            {
                if (ViewState["CursorAt"] == null) return "";
                return (string)ViewState["CursorAt"];
            }

            set
            {
                ViewState["CursorAt"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Delay
        {
            get
            {
                if (ViewState["Delay"] == null) return 0;
                return (int)ViewState["Delay"];
            }

            set
            {
                ViewState["Delay"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Distance
        {
            get
            {
                if (ViewState["Distance"] == null) return 1;
                return (int)ViewState["Distance"];
            }

            set
            {
                ViewState["Distance"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Grid
        {
            get
            {
                if (ViewState["Grid"] == null) return "";
                return (string)ViewState["Grid"];
            }

            set
            {
                ViewState["Grid"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Handle
        {
            get
            {
                if (ViewState["Handle"] == null) return "";
                return (string)ViewState["Handle"];
            }

            set
            {
                ViewState["Handle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Helper
        {
            get
            {
                if (ViewState["Helper"] == null) return "";
                return (string)ViewState["Helper"];
            }

            set
            {
                ViewState["Helper"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string IFrameFix
        {
            get
            {
                if (ViewState["IFrameFix"] == null) return "";
                return (string)ViewState["IFrameFix"];
            }

            set
            {
                ViewState["IFrameFix"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public float Opacity
        {
            get
            {
                if (ViewState["Opacity"] == null) return 0;
                return (float)ViewState["Opacity"];
            }

            set
            {
                ViewState["Opacity"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool RefreshPositions
        {
            get
            {
                if (ViewState["RefreshPositions"] == null) return false;
                return (bool)ViewState["RefreshPositions"];
            }

            set
            {
                ViewState["RefreshPositions"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public RevertOptions Revert
        {
            get
            {
                if (ViewState["Revert"] == null) return RevertOptions.no;
                return (RevertOptions)ViewState["Revert"];
            }

            set
            {
                ViewState["Revert"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int RevertDuration
        {
            get
            {
                if (ViewState["RevertDuration"] == null) return 500;
                return (int)ViewState["RevertDuration"];
            }

            set
            {
                ViewState["RevertDuration"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Scope
        {
            get
            {
                if (ViewState["Scope"] == null) return "";
                return (string)ViewState["Scope"];
            }

            set
            {
                ViewState["Scope"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Scroll
        {
            get
            {
                if (ViewState["Scroll"] == null) return false;
                return (bool)ViewState["Scroll"];
            }

            set
            {
                ViewState["Scroll"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ScrollSensitivity
        {
            get
            {
                if (ViewState["ScrollSensitivity"] == null) return 20;
                return (int)ViewState["ScrollSensitivity"];
            }

            set
            {
                ViewState["ScrollSensitivity"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ScrollSpeed
        {
            get
            {
                if (ViewState["ScrollSpeed"] == null) return 20;
                return (int)ViewState["ScrollSpeed"];
            }

            set
            {
                ViewState["ScrollSpeed"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Snap
        {
            get
            {
                if (ViewState["Snap"] == null) return "";
                return (string)ViewState["Snap"];
            }

            set
            {
                ViewState["Snap"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public SnapModes SnapMode
        {
            get
            {
                if (ViewState["SnapMode"] == null) return SnapModes.both;
                return (SnapModes)ViewState["SnapMode"];
            }

            set
            {
                ViewState["SnapMode"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int SnapTolerance
        {
            get
            {
                if (ViewState["SnapTolerance"] == null) return 20;
                return (int)ViewState["SnapTolerance"];
            }

            set
            {
                ViewState["SnapTolerance"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Stack
        {
            get
            {
                if (ViewState["Stack"] == null) return bool.FalseString;
                return (string)ViewState["Stack"];
            }

            set
            {
                ViewState["Stack"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ZIndex
        {
            get
            {
                if (ViewState["ZIndex"] == null) return int.MinValue;
                return (int)ViewState["ZIndex"];
            }

            set
            {
                ViewState["ZIndex"] = value;
            }
        }



        protected override void Render(HtmlTextWriter writer)
        {
            if(!AddClasses)  AddParameter("AddClasses", "false");
            if (AppendTo!="") AddParameter("appendTo", "'" + AppendTo + "'");
            if (Axis!= AxisOptions.none) AddParameter("axis", "'" + Axis.ToString() + "'");
            if (Cancel!="") AddParameter("cancel", "'" + Cancel + "'");
            if (ConnectToSortable != "") AddParameter("connectToSortable", "'" + ConnectToSortable + "'");
            if (Containment != "") AddParameter("containment", "'" + Containment + "'");
            if (Cursor != "") AddParameter("cursor", "'" + Cursor + "'");
            if (Delay!=0) AddParameter("delay", "" + Delay + "");
            if (Distance!=1) AddParameter("distance", "" + Distance + "");
            if (Grid != "") AddParameter("grid", "[" + Grid + "]");
            if (Handle != "") AddParameter("handle", "'" + Handle + "'");
            if (Helper != "") AddParameter("helper", "'" + Helper + "'");
            if (AppendTo != "") AddParameter("appendTo", "'" + AppendTo + "'");
            if (IFrameFix != "")
            {
                if (IFrameFix == bool.TrueString || IFrameFix == bool.FalseString)
                    AddParameter("iframeFix", IFrameFix.ToLower());
                else
                    AddParameter("iframeFix", "'" + IFrameFix + "'");
            }
            if (Opacity!=0) AddParameter("opacity", "'" + Opacity + "'");
            if (RefreshPositions) AddParameter("refreshPositions", "true");
            if (Revert!= RevertOptions.no)
            {
                if (Revert == RevertOptions.no)
                    AddParameter("revert", "false");
                else if (Revert == RevertOptions.yes)
                    AddParameter("revert", "true");
                else
                    AddParameter("revert", "'" + Revert + "'");
            }
            if (RevertDuration!=500) AddParameter("revertDuration", "'" + RevertDuration + "'");
            if (Scroll) AddParameter("scroll", "true");
            if (ScrollSensitivity!=20) AddParameter("scrollSensitivity", "'" + ScrollSensitivity + "'");
            if (ScrollSpeed!=20) AddParameter("scrollSpeed", "'" + ScrollSpeed + "'");
            if (Snap != "")
            {
                if (Snap == bool.TrueString || Snap == bool.FalseString)
                    AddParameter("snap", Snap.ToString().ToLower());
                else
                    AddParameter("snap", "'" + Snap + "'");
            }
            if (SnapMode!= SnapModes.both) AddParameter("snapMode", "'" + SnapMode.ToString() + "'");
            if (SnapTolerance!=20) AddParameter("snapTolerance", SnapTolerance.ToString());
            
            if(Stack != bool.FalseString)
                AddParameter("stack", "'" + Stack + "'");

            if (ZIndex!=int.MinValue) 
                AddParameter("zIndex", "'" + ZIndex.ToString() + "'");


            AddBinding("dragstart", OnClientDragStart);
            AddBinding("drag", OnClientDrag);
            AddBinding("dragstop", OnClientDragStop);
            base.Render(writer);
        }
    }
}
